<?php 
	require_once("init.php");  
?>

<?php 
	$userId = 1;
	if (isset($_SESSION["userId"])) {
		$userId = $_SESSION["userId"];
	}

	$strCheckModeration = "
		SELECT voteId
		FROM votes
		WHERE topicId = ".$_GET["id"]."
	";
	if (isset($_SESSION["userId"])) {
		$strCheckModeration = $strCheckModeration."
			AND userId = ".$_SESSION["userId"]."
		";
	} else if (strlen(session_id()) > 0) {
		$strCheckModeration = $strCheckModeration."
			AND sessionId = '".session_id()."'
		";
	} else {
		$strCheckModeration = $strCheckModeration."
			AND 1 = 0
		";
	}
	$qCheckModeration = mysql_query($strCheckModeration);
	
	// if vote is already made, display error message
	if (mysql_num_rows($qCheckModeration) > 0) {
?>

<div id="moderate-fail" title="Oops!" class="panel">
	<p>You have previously reported or moderated this topic.</p>
</div>

<?php
	} else {

		$userId = 1;
		if (isset($_SESSION["userId"])) {
			$userId = $_SESSION["userId"];
		}
		// Add user's moderation
		$qInsertModeration = mysql_query("
			INSERT INTO moderations (
				topicId,
				userId,
				sessionId,
				favor,
				dateModerated
			)
			VALUES (
				".$_GET["id"].",
				".$userId.",
				'".session_id()."',
				".$_GET["a"].",
				now()
			)
		");
		
		// Update the number
		$qUpdateTopicPoints = mysql_query("
			UPDATE topics
			SET 
				points = points + ".$_GET["a"]."
			WHERE topicId = '".$_GET["id"]."'
		");
		
		// Update user points
		if (isset($_SESSION["userId"])) {
			$qUpdateUserPoints = mysql_query("
				UPDATE users
				SET 
					points = points + ".$moderateTopicPoints."
				WHERE 
					userId = ".$_SESSION["userId"]."
			");
		}
?>

<form id="moderateTopicSuccess" class="dialog" method="post" target="_self" action="redirect.php?url=index.php">
	<fieldset>
    <h1>Thank you!</h1>
    <p align="center">Your moderation has been submitted.</p>
    <p align="center"><input class="submit" type="submit" name="submit" value="Continue" /></p>
  </fieldset>
</form>

<?php
	}
?>

<?php 
	require_once("end.php"); 
?>